<?php

use yii\widgets\LinkPager;
use common\components\Word;
use yii\helpers\Url;
use common\models\Menu;

/* @var $this yii\web\View */
/* @var $dataProvider yii\data\ActiveDataProvider */

$this->title = '菜单管理';
$this->params['breadcrumbs'][] = $this->title;

/* 获取一级菜单列表 */
$menu = new Menu();
$listMenu = $menu->getTop();
?>
<hr/>
<div class="menu-index">

	<div style="margin-bottom: 10px">

		<a class="btn btn-success" href="<?= Url::to(['sys/create']) ?>">新增菜单</a>

		<a class="btn btn-danger" href="javascript:GetCheckbox()">删除菜单</a>

		<form class="navbar-form navbar-right search" action="<?= Url::to(['sys/index']) ?>" method="get"  style="margin-top: 0">
			<div class="input-group">
				<div class="input-group-btn">
					<button type="button" class="btn btn-default dropdown-toggle" data-toggle="dropdown" aria-expanded="false">Search <span class="caret"></span></button>
					<ul class="dropdown-menu" role="menu">
						<?php foreach($listMenu as $menu): ?>
							<li><a href="<?= Url::to(['', 'groups' => $menu->groups]) ?>"><?= $menu->title ?></a></li>
						<?php endforeach ?>
					</ul>
				</div>

				<input type="text" class="form-control" name="title" placeholder="标题名称">
				<span class="input-group-btn">
					<button type="submit" class="btn btn-default">
						<i class="fa fa-search"></i>
					</button>
				</span>
			</div>
		</form>

<!-- get方式的搜索 -->
		<!--<script>

			function search(){
				var url = $('.search').attr('action');
				var name = $('input[name=title]').val();
				location.href = url + '&title=' + name;
			}
			$('.search').find('input:text').keyup(function(event){
				if(event.keyCode===13){
					search();
				}
			});

		</script>-->

	</div>

	<div id="w0" class="grid-view">

		<div class="summary">
			第<b><?= $pages->offset + 1 ?>-<?= $pages->offset + count($list) ?></b>条，共<b><?= $pages->totalCount ?></b>条数据
		</div>

		<table class="table table-striped table-bordered">
			<thead>
			<tr>
				<th>
					<input type="checkbox" class="select-on-check-all" name="id_all" value="1">
				</th>
				<th>
					<a href="javascript:">
						ID
					</a>
				</th>
				<th>
					<a href="javascript:">
						标题
					</a>
				</th>
				<th>
					<a href="javascript:">
						URL
					</a>
				</th>
				<th>
					<a href="javascript:">
						排序
					</a>
				</th>
				<th>
					<a href="javascript:">
						上级菜单
					</a>
				</th>
				<th>
					<a href="javascript:">
						创建时间
					</a>
				</th>
				<th>
					<a href="javascript:">
						是否隐藏
					</a>
				</th>
				<th>
					<a href="javascript:">
						状态
					</a>
				</th>
				<th>
					<a href="javascript:">
						操作
					</a>
				</th>
			</tr>
			</thead>

			<tbody>
			<?php foreach ($list as $val): ?>
			<tr>
				<td>
					<input type="checkbox" name="id[]" value="<?= $val->id ?>">
				</td>
				<td>
					<?= $val->id ?>
				</td>
				<td>
					<?= $val->title ?>
				</td>
				<td>
					<?= $val->url ?>
				</td>
				<td>
					<?= $val->sort ?>
				</td>
				<td>
					<?= $val->pname ?>
				</td>
				<td>
					<?= Word::time_format($val->create_time) ?>
				</td>
				<td>
					<?= $val->hide ? '<a type="button" class="btn btn-danger btn-xs" role="button">隐藏</a>' : '<a type="button" class="btn btn-success btn-xs" role="button">显示</a>'  ?>
				</td>
				<td>
					<?= $val->status ? '<a type="button" class="btn btn-success btn-xs" role="button">正常</a>' : '<a type="button" class="btn btn-danger btn-xs" role="button">禁用</a>'  ?>
				</td>
				<td>
					<a href="<?= Url::to(['sys/update', 'id' => $val->id]) ?>" title="更新" data-pjax="0">
						<span class="glyphicon glyphicon-pencil"></span>
					</a>
					<a href="<?= Url::to(['sys/delete', 'id' => $val->id]) ?>" title="删除" data-confirm="您确定要删除此项吗？" data-method="post" data-pjax="0">
						<span class="glyphicon glyphicon-trash"></span>
					</a>
				</td>
			</tr>
			<?php endforeach ?>
			</tbody>
		</table>

		<?= LinkPager::widget([
			'pagination' => $pages
		]) ?>

	</div>

</div>

<script type="text/javascript">

	var GetCheckbox = function (){
		if(confirm('确定要删除吗？')) {
			var data = '';
			var list = $("input:checkbox[name='id[]']");

			for (var i = 0; i < list.length; i++) {
				if(list[i].checked){
					data += list[i].value + ',';
				}
			};
			if (data.length > 0) {
				$.post('<?php echo Url::to(['sys/delete']);?>', {id: data}, function (data) {
					location.href = '';
				});
			} else {
				alert("请选择要删除的关键字!");
			}
		}
	}

	<?php $this->beginBlock('js_end') ?>

$(function(){
	highlight_subnav("<?= Url::to(['sys/index']) ?>");
});

	<?php $this->endBlock() ?>
</script>

<?php $this->registerJs($this->blocks['js_end'], \yii\web\View::POS_END); ?>
